﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PMSytem.Windows.App.Pages
{
    /// <summary>
    /// Interaction logic for us_Photos.xaml
    /// </summary>
    public partial class us_Photos : UserControl, INotifyPropertyChanged
    {
        public us_Photos()
        {
            InitializeComponent();
            DataContext = new RecordImageSource();
        }

        private RecordImageSource p;

        public RecordImageSource Context
        {
            get
            {
                // if(p==null)
                //    p = new Person() { FirstName="XX",LastName="YY"};
                return p;
            }
            set
            {
                p = value;
                OnPropertyChanged("Context");
                // var tct = txtBox.Text;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(String property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public static ImageSource ByteToImage(byte[] imageData)
        {
            BitmapImage biImg = new BitmapImage();
            MemoryStream ms = new MemoryStream(imageData);
            biImg.BeginInit();
            biImg.StreamSource = ms;
            biImg.EndInit();

            ImageSource imgSrc = biImg as ImageSource;

            return imgSrc;
        }

        private void CreateThumbnail()
        {
            // Create an Image control
            Image thumbnailImage = new Image();
            thumbnailImage.Width = 130;

            // Create a BitmapImage and sets its DecodePixelWidth and DecodePixelHeight
            BitmapImage bmpImage = new BitmapImage();
            bmpImage.BeginInit();
            bmpImage.UriSource = new Uri(@"Garden.jpg", UriKind.RelativeOrAbsolute);
            bmpImage.DecodePixelWidth = 120;
            bmpImage.DecodePixelHeight = 120;
            bmpImage.EndInit();
            // Set Source property of Image
            thumbnailImage.Source = bmpImage;

            //LayoutRoot.Children.Add(thumbnailImage);
        }

        private void OnPhotoClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        }

        private void btnDelete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        }

       

        private void PhotosView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(PhotosView.SelectedIndex >0)
                btnDelete.IsEnabled = true;
            else
                btnDelete.IsEnabled = false;
        }

       
    }

    public class RecordImageSource : INotifyPropertyChanged
    {
        private List<ImageSource> _list;

        public List<ImageSource> PhotoList
        {
            get
            {
                return _list;
            }
            set
            {
                _list = value;
                this.OnPropertyChanged("PhotoList");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(
                    this, new PropertyChangedEventArgs(propName));
        }

        #endregion INotifyPropertyChanged Members
    }
}